﻿Option Strict On
Option Explicit On

Partial Class private_RegUserPlan
    Inherits System.Web.UI.Page
    Dim u As MembershipUser
    Dim userplan As Integer

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Add code here to present the right options to the user based on the choice they have made so far

        'Not all of these will be visible depending on the choices
        'lblTrialPlan.Visible = True
        u = Membership.GetUser(User.Identity.Name)
        Dim checkuserplanTA As New LogginsLogDSTableAdapters.usp_UserCheckTableAdapter
        Dim checkuserplanDT As LogginsLogDS.usp_UserCheckDataTable

        checkuserplanDT = checkuserplanTA.GetData(CType(u.ProviderUserKey, Global.System.Guid?))
        For Each usercheckRow As LogginsLogDS.usp_UserCheckRow In checkuserplanDT
            If usercheckRow.UserPlanChosen Then
                imgTrialPlan.Enabled = False
            End If
        Next
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        Dim usageplanDT As LogginsLogDS.usp_UsagePlanDataTable
        usageplanDT = usageplanTA.GetData()
        Dim planstring As Integer = (From plan In usageplanDT.AsEnumerable() Where plan.UserId = (CType(u.ProviderUserKey, Global.System.Guid?)) Order By plan.UsagePlanId Descending Select plan.UsagePlanId).FirstOrDefault()


        If planstring = 1 Then
            imgTrialPlan.Enabled = False
            lblTrialPlan.Text = "Current plan"
            lblBasicPlan.Text = "Upgrade "
            lblBronzePlan.Text = "Upgrade "
            lblSilverPlan.Text = "Upgrade "
            lblGoldPlan.Text = "Upgrade "

        ElseIf planstring = 2 Then
            imgTrialPlan.Enabled = False
            imgBasicPlan.Enabled = False
            lblTrialPlan.Visible = False
            lblBasicPlan.Text = "Current plan"
            lblBronzePlan.Text = "Upgrade "
            lblSilverPlan.Text = "Upgrade "
            lblGoldPlan.Text = "Upgrade "

        ElseIf planstring = 3 Then
            imgTrialPlan.Enabled = False
            imgBasicPlan.Enabled = False
            imgBronzePlan.Enabled = False
            lblTrialPlan.Visible = False
            lblBasicPlan.Visible = False
            lblBronzePlan.Text = "Current plan"
            lblSilverPlan.Text = "Upgrade "
            lblGoldPlan.Text = "Upgrade "


        ElseIf planstring = 4 Then
            imgTrialPlan.Enabled = False
            imgBasicPlan.Enabled = False
            imgBronzePlan.Enabled = False
            imgSilverPlan.Enabled = False
            lblTrialPlan.Visible = False
            lblBasicPlan.Visible = False
            lblBronzePlan.Visible = False
            lblSilverPlan.Text = "Current plan"
            lblGoldPlan.Text = "Upgrade "

        ElseIf planstring = 5 Then
            imgTrialPlan.Enabled = False
            imgBasicPlan.Enabled = False
            imgBronzePlan.Enabled = False
            imgSilverPlan.Enabled = False
            imgGoldPlan.Enabled = False
            lblTrialPlan.Visible = False
            lblBasicPlan.Visible = False
            lblBronzePlan.Visible = False
            lblSilverPlan.Visible = False
            lblGoldPlan.Text = "Current plan"
        Else
            lblTrialPlan.Text = "Upgrade"
            lblBasicPlan.Text = "Upgrade "
            lblBronzePlan.Text = "Upgrade "
            lblSilverPlan.Text = "Upgrade "
            lblGoldPlan.Text = "Upgrade "

        End If
    End Sub

    Protected Sub imgTrialPlan_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgTrialPlan.Click
        DeactivatePlans()
        Dim checkuserplanTA As New LogginsLogDSTableAdapters.usp_UserCheckTableAdapter
        Dim checkuserplanDT As LogginsLogDS.usp_UserCheckDataTable

        checkuserplanDT = checkuserplanTA.GetData(CType(u.ProviderUserKey, Global.System.Guid?))
        For Each usercheckRow As LogginsLogDS.usp_UserCheckRow In checkuserplanDT
            If usercheckRow.UserPlanChosen Then
                checkuserplanTA.Update(usercheckRow.UserId, usercheckRow.AccountInCredit, usercheckRow.ApprovalRequired, usercheckRow.RegistrationComplete, usercheckRow.EmailConfirmed, usercheckRow.AwaitingApproval, usercheckRow.AccountActive, usercheckRow.UserPlanChosen, usercheckRow.Active, usercheckRow.UserRoleSet, usercheckRow.AssociatedEntitySet, usercheckRow.Id, CType(1, Boolean?))
            End If
        Next
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        usageplanTA.Insert(CType(u.ProviderUserKey, Global.System.Guid?), 1, CType(1, Boolean?), DateTime.Now, Nothing)
        Response.Redirect("~/private/RegUserPlan.aspx")
    End Sub

    Protected Sub imgBasicPlan_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgBasicPlan.Click
        DeactivatePlans()
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        usageplanTA.Insert(CType(u.ProviderUserKey, Global.System.Guid?), 2, CType(1, Boolean?), DateTime.Now, Nothing)
        Response.Redirect("~/private/RegUserPlan.aspx")
    End Sub

    Protected Sub imgBronzePlan_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgBronzePlan.Click
        DeactivatePlans()
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        usageplanTA.Insert(CType(u.ProviderUserKey, Global.System.Guid?), 3, CType(1, Boolean?), DateTime.Now, Nothing)
        Response.Redirect("~/private/RegUserPlan.aspx")
    End Sub

    Protected Sub imgSilverPlan_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgSilverPlan.Click
        DeactivatePlans()
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        usageplanTA.Insert(CType(u.ProviderUserKey, Global.System.Guid?), 4, CType(1, Boolean?), DateTime.Now, Nothing)
        Response.Redirect("~/private/RegUserPlan.aspx")
    End Sub

    Protected Sub imgGoldPlan_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgGoldPlan.Click
        DeactivatePlans()
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        usageplanTA.Insert(CType(u.ProviderUserKey, Global.System.Guid?), 5, CType(1, Boolean?), DateTime.Now, Nothing)
        Response.Redirect("~/private/RegUserPlan.aspx")
    End Sub
    Sub DeactivatePlans()
        Dim usageplanTA As New LogginsLogDSTableAdapters.usp_UsagePlanTableAdapter()
        Dim usageplanDT As LogginsLogDS.usp_UsagePlanDataTable
        usageplanDT = usageplanTA.GetData()
        Dim usage = From usageplan In usageplanDT.AsEnumerable() Where usageplan.UserId = CType(u.ProviderUserKey, Global.System.Guid?) AndAlso usageplan.Active = True Select usageplan
        For Each usageRow As LogginsLogDS.usp_UsagePlanRow In usage
            usageplanTA.Update(usageRow.UserId, usageRow.UsagePlanId, False, usageRow.DatePlanStarted, DateTime.Now, usageRow.Id)
        Next
    End Sub

End Class
